<?php
class ManageController extends AdminController{
    public function init() {
        $this->tableName='ad';
        parent::init();
    }

    public function indexAction() {
        $this->list=$this->model->field('id,name,intro,width,height,create_user_id,create_time,update_user_id,update_time,status,key')->getlist();
        $this->display();
    }

    public function addAction() {
        if (IS_POST){
            $param['name']=I('name','str','');
            if (!$param['name']){
                $this->error('请输入链接名称');
            }
            $param['key']=I('key','en','');
            if (!$param['key']){
                $this->error('请输入链接地址');
            }
            $param['width']=I('width','int',0);
            $param['height']=I('height','int',0);
            $param['code']=I('code','str','');
            $param['intro']=I('intro','str','');
            $param['status']=I('status','int',1);
            $param['type']=I('type','int',1);
            $param['create_user_id']=$_SESSION['admin']['userid'];
            $param['create_time']=NOW_TIME;
            if($this->model->add($param)){
                $this->success('添加成功',U('index'));
            }else{
                $this->error('添加失败');
            }
        }
        $this->display();
    }

    public function editAction() {
        $id=I('request.id','int',0);
        $info=$this->model->where(array('id'=>$id))->find();
        if (IS_POST){
            $param['name']=I('name','str','');
            if (!$param['name']){
                $this->error('请输入链接名称');
            }
            $param['width']=I('width','int',0);
            $param['height']=I('height','int',0);
            $param['code']=I('code','str','');
            $param['intro']=I('intro','str','');
            $param['status']=I('status','int',1);
            $param['type']=I('type','int',1);
            $param['update_user_id']=$_SESSION['admin']['userid'];
            $param['update_time']=NOW_TIME;
            $param['id']=$id;
            if ($this->model->edit($param)){
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败');
            }
        }
        $this->info=$info;
        $this->display();
    }

    public function showAction() {
        $id=I('request.id','int',0);
        $this->info=$this->model->where(array('id'=>$id))->find();
        $this->display();
    }
}